
//-------------------------------------------------------------
//-------------------------------------------------------------
user_add.Instances = null;
//-------------------------------------------------------------
user_add.CreateObject = function(config) {
    var obj = null;
    if (user_add.Instances != null) {
        obj = user_add.Instances[config.id];
    }
    if (obj == null) {
        if (user_add.Instances == null) {
            user_add.Instances = new Object();
        }
        obj = new user_add(config);
        user_add.Instances[config.id] = obj;
    }
    //obj.init();

    return obj;
}
//-------------------------------------------------------------
function user_add(config) {
    var Me = this;
    this.id = config.id;
    this.FOLDER_UPLOAD_TEMP = config.FOLDER_UPLOAD_TEMP;
    this.USER_TYPE_Employee = config.USER_TYPE_Employee;
    this.USER_TYPE_Resident = config.USER_TYPE_Resident;

    //------------------------------------------------------------
    this.init = function() {
        $('#permission_id').multiSelect({
            selectableOptgroup: true
        });
        $('#selectallper').click(function() {
            $('#permission_id').multiSelect('select_all');
            return false;
        });
        $('#deselectallper').click(function() {
            $('#permission_id').multiSelect('deselect_all');
            return false;
        });
        $('#group_id').multiSelect({
            selectableOptgroup: true
        });
        $('#selectallgroup').click(function() {
            $('#group_id').multiSelect('select_all');
            return false;
        });
        $('#deselectallgroup').click(function() {
            $('#group_id').multiSelect('deselect_all');
            return false;
        });
        $('[data-rel=tooltip]').tooltip();

        $(".select2").css('width', '200px').select2({allowClear: true})
                .on('change', function() {
                    $(this).closest('form').validate().element($(this));
                });

        $.mask.definitions['~'] = '[+-]';

        jQuery.validator.addMethod("Employee", function(value, element) {
            if (jQuery("#usertype").val() == Me.USER_TYPE_Employee) {
                return jQuery.trim(value) != "";
            }
            return true;
        }, "This field is required.");

        var fb = true;
        $('#validation-form').validate({
            errorElement: 'div',
            errorClass: 'help-block',
            focusInvalid: false,
            rules: {
                email: {
                    email: true
                },
                outsideaccess: {
                    required: true
                },
                fname: {
                    required: true
                },
                location_id: {
                    required: true
                },
                
                username: {
                    email: true,
                    required: true
                },
                password: {
                    required: true
                }
            },
            messages: {
                email: {
                    required: "Please provide a valid email.",
                    email: "Please provide a valid email."
                }
            },
            invalidHandler: function(event, validator) { //display error alert on form submit   
                $('.alert-danger', $('.login-form')).show();
            },
            highlight: function(e) {
                if (fb) {
                    $(e).focus();
                    fb = false;
                }
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },
            success: function(e) {
                $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
                $(e).remove();

            },
            errorPlacement: function(error, element) {
                if (element.is(':checkbox') || element.is(':radio')) {
                    var controls = element.closest('div[class*="col-"]');
                    if (controls.find(':checkbox,:radio').length > 1)
                        controls.append(error);
                    else
                        error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
                }
                else if (element.is('.select2')) {
                    error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
                }
                else if (element.is('.chosen-select')) {
                    error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
                }
                else
                    error.insertAfter(element.parent());
            },
            submitHandler: function(form) {
                //save
                _ajax.url = url_base + "admin/user/edit";
                _ajax.data = jQuery(form).serialize();
                _ajax.loading(true);
                _ajax.method = 'POST';
                jQuery('.wizard-actions button').hide();
                jQuery("#loading").show();
                _ajax.success = function(data) {
                    _ajax.loading(false);
                    if (data.error == 1) {
                        jQuery("#" + data.id).parent().parent().append('<div class="help-block" for="' + data.id + '">' + data.msg + '</div>');
                        jQuery("#" + data.id).parent().parent().parent().addClass('has-error');
                        jQuery("#" + data.id).focus();
                    } else {
                        _user_list.get_data(this.page, function() {
                            show_msg_success(data.msg);
                            jQuery('#divList').show();
                            jQuery('#divEdit').hide();
                        });
                    }
                    jQuery('.wizard-actions button').show();
                    jQuery("#loading").hide();
                }
                _ajax.show();
            },
            invalidHandler: function(form) {
            }
        });

        $('#uploadavatar').ajaxupload({
            url: url_base_media + '/upload_file.php',
            remotePath: Me.FOLDER_UPLOAD_TEMP,
            maxFiles: 1,
            //editFilename:true,
            autoStart: true,
            bootstrap: true,
            allowExt: ['png', 'jpg','gif'],
            maxFileSize: "10M",
            removeOnSuccess: true,
            data: 'type=user&u=' + user_id + "&session=" + session,
            success: function(name) {
                jQuery("#imageuploadcontent").show();
                jQuery("#imageuploadcontent").attr("src", url_base_media + "/" + Me.FOLDER_UPLOAD_TEMP + "/" + name)
                jQuery("#imagename").val(name);
            },
            error: function(txt, fileName) {
                jQuery("#cmdSave").removeAttr("disabled", "disabled");
            },
            onInit: function(au)
            {
                //hide remove files and upload button
                au.removeFiles.hide();
                au.uploadFiles.hide();
                jQuery(".ax-legend").hide();
            }

        });

        $('#dob').datepicker({autoclose: true}).next().on(ace.click_event, function() {
            $(this).prev().focus();
        });

        jQuery("#cmdsave").click(function() {
            fb = true;
            $('#validation-form').submit();
        });
        jQuery("#location_id").unbind('change').bind('change', Me.location_id_change);
        jQuery("input:radio[name='outsideaccess']").unbind('click', Me.usertype_click).bind('click', Me.usertype_click);
        if (jQuery(".outsideaccesscheck").is(":checked")) {
            //jQuery("#divEmployees").show();
            //jQuery("#divEmployeesGroupPermissionEdit").show();
        }


    }
    this.usertype_click = function() {
        if (jQuery(this).val() == 1) {
            //jQuery("#divEmployees").show();
            //jQuery("#divEmployeesGroupPermissionEdit").show();
        } else {
            //jQuery("#divEmployees").hide();
            //jQuery("#divEmployeesGroupPermissionEdit").hide();
        }
    }
    this.location_id_change = function() {
        _ajax.url = url_base + "admin/user/get_department?location=" + jQuery(this).val();
        _ajax.data = "";
        _ajax.loading(true);
        _ajax.method = 'POST';
        _ajax.success = function(data) {
            jQuery("#department_id").html(data);
            _ajax.loading(false);
        }
        _ajax.show();
    }

    //javascript code here
}


